<?php

require_once BaseUtil::getModulePath("dbgrid") . '/DBGrid.php';
require_once BaseUtil::getModulePath("dbgrid") . '/DBGridConstant.php';
require_once BaseUtil::getModulePath("base") . '/services/BaseService.php';
require_once BaseUtil::getModulePath("config") . '/models/MarketingMappingDao.php';

class MarketingMappingService extends BaseService {
	
	private $marketingmappingDao;
	
	public function __construct() {
		parent::__construct();
		$this->marketingmappingDao = new MarketingMappingDao();
		$this->marketingmappingDao->setDb($this->db);
	}
	
	public function getMappings($view,$form) {
		$dbGrid = new DBGrid($view);
		$dbGrid->setPost($form);
		$dbGrid->setModule("/config/marketingmapping");
		$dbGrid->addValueFilter("is_delete",0);
		$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,'mapping_key',$this->_translate->translate("mapping_key"));
		$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,'mapping_value',$this->_translate->translate("mapping_value"));
		$dbGrid->addFetchTable("marketing_mappings","*");
		$dbGrid->addFetchColumn("mapping_key",$this->_translate->translate("mapping_key"),"edit");
		$dbGrid->addFetchColumn("mapping_value",$this->_translate->translate("mapping_value"));
		$dbGrid->addFetchColumn("created",$this->_translate->translate("createDate"));
		//module action
		$dbGrid->addAAModuleAction("marketingmapping","edit",$this->_translate->translate("editBtn"),"edit");
		$dbGrid->addAAModuleAction("marketingmapping","delete",$this->_translate->translate("delBtn"),"deletesome");
		$dbGrid->addAAModuleAction("marketingmapping","add",$this->_translate->translate("addBtn"),"add");
		$dbGrid->setPageTitle($this->_translate->translate("searchTitle"));
		return $dbGrid->render();
	}
	
	public function addMapping($form) {
		$mapping = array();
		$mapping["mapping_key"] = $form->getValue("mapping_key");
		$mapping["mapping_value"] = $form->getValue("mapping_value");
		$this->marketingmappingDao->insertData($mapping);
	}
	
	public function editMapping($form) {
		$mapping = array();
		$mapping["mapping_key"] = $form->getValue("mapping_key");
		$mapping["mapping_value"] = $form->getValue("mapping_value");
		$this->marketingmappingDao->updateMapping($mapping,$form->getValue("id"));
	}
	
	public function getMappingById($id) {
		return $this->marketingmappingDao->getByCol("id",$id);
	}
	
	public function deleteMappings($ids) {
		foreach($ids as $id){
			if(!empty($id)){
				$this->marketingmappingDao->deleteMapping($id);
			}
		}
	}

	public function delete($ids) {
		if(!empty($ids)){
			$this->marketingmappingDao->deleteMapping($ids);
		}
	}
}